package com.jing.weibo.service.user;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jing.system.comm.contants.SystemCons;
import com.jing.system.model.PageCls;
import com.jing.weibo.mapper.user.UserCwMpMapper;
import com.jing.weibo.pojo.fans.Fansattention;
import com.jing.weibo.pojo.user.UserCwMp;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.fans.FansAttentionService;

/**
 * 可同城互粉的用户的Service
 * @author jing.yue
 * @version 2012/11/13 1.0.0
 */
@Component
public class UserCwMpService {

	@Autowired
	private UserCwMpMapper userCwMpMapper;
	@Autowired
	private FansAttentionService fansAttentionService;

	/**
	 * 保存可同城互粉用户信息
	 * @param userInfo
	 * @throws Exception
	 */
	public void saveUserCwMp(UserInfo userInfo) throws Exception {
		UserCwMp userCwMp = new UserCwMp();
		userCwMp.setUsername(userInfo.getUsername());
		userCwMp.setNickname(userInfo.getNickname());
		userCwMp.setToken(userInfo.getToken());
		userCwMp.setAttentionnum(userInfo.getAttentionnum());
		userCwMp.setFansnum(userInfo.getFansnum());
		userCwMp.setGender(userInfo.getGender());
		userCwMp.setWeibonum(userInfo.getWeibonum());
		userCwMp.setImg(userInfo.getImg());
		userCwMp.setVerified(userInfo.getVerified());
		userCwMp.setLocation(userInfo.getLocation());
		userCwMp.setVipareaid(SystemCons.USER_CW_MP_VIPAREA_ID_DEFAULT);
		userCwMpMapper.saveUserCwMp(userCwMp);
	}

	/**
	 * 根据用户名查询对象
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public UserCwMp findByUsername(String username) throws Exception {
		return userCwMpMapper.findByUsername(username);
	}

	/**
	 * 根据用户名修改信息
	 * @param userInfo
	 * @throws Exception
	 */
	public void updateUserCwMp(UserInfo userInfo) throws Exception {
		UserCwMp userCwMp = new UserCwMp();
		userCwMp.setUsername(userInfo.getUsername());
		userCwMp.setNickname(userInfo.getNickname());
		userCwMp.setToken(userInfo.getToken());
		userCwMp.setAttentionnum(userInfo.getAttentionnum());
		userCwMp.setFansnum(userInfo.getFansnum());
		userCwMp.setGender(userInfo.getGender());
		userCwMp.setWeibonum(userInfo.getWeibonum());
		userCwMp.setImg(userInfo.getImg());
		userCwMp.setVerified(userInfo.getVerified());
		userCwMp.setLocation(userInfo.getLocation());
		userCwMpMapper.updateUserCwMp(userCwMp);
	}

	/**
	 * 根据用户名删除可同城互粉用户信息
	 * @param username
	 * @throws Exception
	 */
	public void deleteByUsername(String username) throws Exception {
		userCwMpMapper.deleteByUsername(username);
	}

	/**
	 * 分页根据用户所在地区获取同城用户信息集合[处理了关注信息等条件]
	 * @param userCwMp
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findByUserInfo(UserCwMp userCwMp) throws Exception {
		List<Fansattention> fas = null;
		List<UserCwMp> userCwMps = userCwMpMapper.findByUserInfo(userCwMp);
		//处理是否关注过了[查询我的关注信息中是否存在这个用户]
		Fansattention fansattention = new Fansattention();
		fansattention.setUsername(userCwMp.getUsername());
		for (UserCwMp ucm : userCwMps) {
			fansattention.setFansattentionusername(ucm.getUsername());
			fas = fansAttentionService.findByUsernameFansname(fansattention);
			if(fas == null || fas.size() == 0) {
				ucm.setIsfa(0);
				ucm.setIsfamp(0);
			} else {
				//当前登录用户关注了该用户
				if("true".equals(fas.get(0).getFollowing())) {
					ucm.setIsfa(1);
				} else {
					ucm.setIsfa(0);
				}
				//该用户关注了当前登录用户
				if("true".equals(fas.get(0).getFollowme())) {
					ucm.setIsfamp(1);
				} else {
					ucm.setIsfamp(0);
				}
			}
		}
		Integer countSize = userCwMpMapper.findByUserInfoCount(userCwMp);
		PageCls pageCls = PageCls.setPageCls(userCwMp.getPage(), userCwMp.getSize(), null, null, countSize, userCwMps.size(), userCwMps);
		return pageCls.getPageInfo();
	}
}